<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\shiyao;

use app\store\controller\Controller;
use app\store\model\shiyao\Crc as CrcModel;
use app\store\model\shiyao\Pi as PiModel;
use app\store\model\shiyao\ProjectCenter as CenterModel;
use app\store\model\shiyao\ProjectTestee as TesteeModel;
use think\facade\Db;

/**
 * 项目控制器
 * Class StoreUser
 * @package app\store\controller
 */
class ProjectCenter extends Controller {

	public function listAll() {
		$model = new CenterModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	public function listUserExport() {
		$user = Db::name('user')->where('user_id', $this->request->param()['userId'])->find();
		if ($user['grade_id'] == 0) {
			$model = new TesteeModel;
			$list = $model->getListUserAll($this->request->param());
		} elseif ($user['grade_id'] == 1) {
			//crc
			$model = new CrcModel;
			$list = $model->getListAll($this->request->param());

		} elseif ($user['grade_id'] == 2) {
			//pi
			$model = new PiModel;
			$list = $model->getListAll($this->request->param());
		} elseif ($user['grade_id'] == 3) {
			//pm
			$model = new CenterModel;
			$list = $model->getListUserAll($this->request->param());
		}
		return $this->renderSuccess(compact('list'));
	}

	public function listUser() {
		$user = Db::name('user')->where('user_id', $this->request->param()['userId'])->find();
		if ($user['grade_id'] == 0) {
			$model = new TesteeModel;
			$list = $model->getListUser($this->request->param());
		} elseif ($user['grade_id'] == 1) {
			//crc
			$model = new CrcModel;
			$list = $model->getList($this->request->param());

		} elseif ($user['grade_id'] == 2) {
			//pi
			$model = new PiModel;
			$list = $model->getList($this->request->param());
		} elseif ($user['grade_id'] == 3) {
			//pm
			$model = new CenterModel;
			$list = $model->getListUser($this->request->param());
		}

		return $this->renderSuccess(compact('list'));
	}

	public function detail(int $centerId) {

		$detail = CenterModel::detail($centerId);
		$pm_ids = Db::name('shiyao_project_center_pm')->where('center_id', $detail['id'])->select()->toArray();
		$detail['pm_ids'] = array_column($pm_ids, 'pm_id');
		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 中心-项目列表
	 * @return array
	 * @throws \think\db\exception\DbException
	 */
	public function list() {
		$model = new CenterModel;
		$list = $model->getListCenter($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 *
	 * @return array
	 */
	public function add() {
		// 新增记录
		$model = new CenterModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}
	public function state(array $goodsIds, bool $state) {
		$model = new CenterModel;
		if (!$model->setStatus($goodsIds, $state)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}

	/**
	 *
	 * @param int $CenterId
	 * @return array
	 */
	public function edit(int $centerId) {
		// 管理员详情
		$model = CenterModel::detail($centerId);
		// 更新记录
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	public function audit(int $CenterId) {
		// 管理员详情
		$model = CenterModel::detail($CenterId);
		// 更新记录
		if ($model->audit($this->postForm())) {
			return $this->renderSuccess('审核成功');
		}
		return $this->renderError($model->getError() ?: '审核失败');
	}

	/**
	 * 删除项目-中心
	 * @param int $CenterId
	 * @return array|bool
	 */

	public function delete(array $centerIds) {
		$model = new CenterModel;
		if (!$model->setDelete($centerIds)) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}

}
